package com.lcc.lynx.common.utils;

import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @author lcc
 * @date 2024/12/17 15:50
 */
@Component
public class RsaUtil {

    private static String publicKey;
    private static String privateKey;

    @Value("${lynx.security.publicKey}")
    public void setPublicKey(String publicKey) {
        RsaUtil.publicKey = publicKey;
    }
    @Value("${lynx.security.privateKey}")
    public void setPrivateKey(String privateKey) {
        RsaUtil.privateKey = privateKey;
    }

    public static RSA getRsa(){
        if (rsa == null) {
            rsa = new RSA(privateKey, publicKey);
        }
        return rsa;
    }

    private static RSA rsa;

    /**
     * 加密
     * @param data
     * @return
     * @author lcc
     * @date 2024/12/17 15:57
     */
    public static String encrypt(String data, KeyType keyType) {
        return getRsa().encryptBase64(data, keyType);
    }

    /**
     * 解密
     * @param data
     * @return
     * @author lcc
     * @date 2024/12/17 15:57
     */
    public static String decrypt(String data, KeyType keyType) {
        return getRsa().decryptStr(data, keyType);
    }
}
